import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '../api/login'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: window.localStorage.getItem('token'),
    refresh_token: window.localStorage.getItem('refreshToken')
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      window.localStorage.setItem('token', tokenStr)
    },
    setRefreshToken (state, refreshToken) {
      state.refresh_token = refreshToken
      window.localStorage.setItem('refreshToken', refreshToken)
    }
  },
  actions: {
    // 发送登录请求
    async asyncLoginAction (store, formLogin) {
      const res = await loginAPI(formLogin)
      const { token, refresh_token: refreshToken } = res.data.data
      console.log(res)
      store.commit('setToken', token)
      store.commit('setRefreshToken', refreshToken)
    }
  },
  modules: {
  }
})
